const express = require("express");
const router = express.Router();
const { success, successWithData } = require("../utils/result");
const { check } = require("express-validator");
const { checkParam } = require("../utils/checkUtils");
const Classification = require("../db/model/classificationModel");

/**
 * 分页查询分类接口
 * @api {POST} /class/queryClassificationList 分页查询分类接口
 * @apiDescription 分页查询分类接口
 * @apiName queryClassificationList
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} page=1   页码
 * @apiBody {Number} limit=10   每页条数
 * @apiParamExample {json} Request-Example:
 *     {
 *        "page": 1,
 *        "limit": "10",
 *     }
 * @apiGroup Class
 * @apiVersion 1.0.0
 */
router.post(
  "/class/queryClassificationList",
  check("page").not().isEmpty(),
  check("limit").not().isEmpty(),
  async function (req, res) {
    checkParam(req);
    const { page, limit, cate_name } = req.body;
    const where = {};
    let offset = (page - 1) * limit;
    if (cate_name) {
      where["cate_name"] = cate_name;
    }
    const result = await Classification.findAndCountAll({
      limit,
      offset,
      where,
    });
    return res.json(successWithData(result));
  }
);

/**
 * 查询所有分类接口
 * @api {POST} /class/queryAllClassificationList 查询所有分类接口
 * @apiDescription 查询所有分类接口
 * @apiName queryAllClassificationList
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiParamExample {json} Request-Example:
 *     {
 *     }
 * @apiGroup Class
 * @apiVersion 1.0.0
 */
router.post("/class/queryAllClassificationList", async function (req, res) {
  const result = await Classification.findAll({
    order: [
      ["sort", "DESC"],
      ["created_time", "DESC"],
    ],
    attributes: ["id", "cate_name"],
  });
  return res.json(successWithData(result));
});

/**
 * @api {POST} /class/add 分类添加
 * @apiDescription 分类添加
 * @apiName add
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {String} cate_name='cate_name'   分类名称
 * @apiBody {String} cate_img='cate_img'   分类图片
 * @apiBody {Number} sort=1   排序
 * @apiParamExample {json} Request-Example:
 *     {
 *        "cate_name": "cate_name",
 *        "cate_img": "cate_img",
 *        "sort": 1
 *     }
 * @apiGroup Class
 * @apiVersion 1.0.0
 */
router.post("/class/add", async function (req, res) {
  await Classification.create({ ...req.body });
  return res.json(success());
});

/**
 * @api {POST} /class/del 分类删除
 * @apiDescription 分类删除
 * @apiName del
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   分类id
 * @apiParamExample {json} Request-Example:
 *     {
 *        "id": 1,
 *     }
 * @apiGroup Class
 * @apiVersion 1.0.0
 */
router.post(
  "/class/del",
  check("id").not().isEmpty(),
  async function (req, res) {
    checkParam(req);
    await Classification.destroy({ where: { id: req.body.id } });
    return res.json(success());
  }
);

/**
 * @api {POST} /class/edit 分类编辑
 * @apiDescription 分类编辑
 * @apiName edit
 * @apiHeader {String} authorization 用户token,格式：Bearer xxxx
 * @apiBody {Number} id=1   分类id
 * @apiBody {String} cate_name   分类名称
 * @apiBody {String} cate_img   分类图片
 * @apiBody {Number} sort   排序
 * @apiParamExample {json} Request-Example:
 *     {
 *        "cate_name": "cate_name",
 *        "cate_img": "cate_img",
 *        "sort": 1
 *     }
 * @apiGroup Class
 * @apiVersion 1.0.0
 */
router.post(
  "/class/edit",
  check("id").not().isEmpty(),
  async function (req, res) {
    checkParam(req);
    await Classification.update(
      { ...req.body },
      { where: { id: req.body.id } }
    );
    return res.json(success());
  }
);

module.exports = router;
